/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.bean_chuyenbay;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author thien_000
 */
@WebServlet(name = "controller_chuyenbay", urlPatterns = {"/controller_chuyenbay"})
public class controller_chuyenbay extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            try {
                String action = request.getParameter("btAction");
                if (action.equals("qlcb")) {
                    bean_chuyenbay cb = new bean_chuyenbay();
                    bean_chuyenbay[] result = cb.hienthichuyenbay();
                    request.setAttribute("INFOCB", result);
                    RequestDispatcher rd = request.getRequestDispatcher("QuanLyChuyenBay.jsp");
                    rd.forward(request, response);
                } else if (action.equals("Thêm mới")) {
                    RequestDispatcher rd = request.getRequestDispatcher("ThemChuyenBay.jsp");
                    rd.forward(request, response);
                } else if (action.equals("Thêm chuyến bay")) {
                    String field_MaCB = request.getParameter("txtMaCB");
                    String field_NgayBay = request.getParameter("txtNgayBay");
                    String field_MaNoiDi = request.getParameter("txtMaNoiDi");
                    String field_MaNoiDen = request.getParameter("txtMaNoiDen");
                    bean_chuyenbay cb = new bean_chuyenbay();
                    String url = "controller_chuyenbay?btAction=qlcb";
                    boolean result = cb.insert(field_MaCB, field_NgayBay, field_MaNoiDi, field_MaNoiDen);
                    if (result) {
                        RequestDispatcher rd = request.getRequestDispatcher(url);
                        rd.forward(request, response);
                    } else {
                        RequestDispatcher rd = request.getRequestDispatcher("Fail_ThemChuyenBay.jsp");
                        rd.forward(request, response);
                    }
                } else if (action.equals("Search")) {
                    String taikhoan = request.getParameter("txtSearch");
                    bean_chuyenbay cb = new bean_chuyenbay();
                    bean_chuyenbay[] result = cb.searchLikeMaCB(taikhoan);
                    request.setAttribute("INFOCB", result);
                    RequestDispatcher rd = request.getRequestDispatcher("QuanLyChuyenBay.jsp");
                    rd.forward(request, response);
                } else if (action.equals("Delete")) {
                    String chuyenbay = request.getParameter("deleteChuyenBay");
                    bean_chuyenbay cb = new bean_chuyenbay();
                    cb.setMaCB(chuyenbay);
                    boolean result = cb.deleteChuyenBay();
                    String url = "controller_chuyenbay?btAction=qlcb";
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                } else if (action.equals("Update")) {
                    String macb=request.getParameter("macb");
                    String ngaybay = request.getParameter("ngaybay");
                    String manoidi = request.getParameter("manoidi");
                    String manoiden = request.getParameter("manoiden");
                    bean_chuyenbay cb = new bean_chuyenbay(macb,ngaybay, manoidi, manoiden);
                    boolean result = cb.updateChuyenBay();
                    String url = "controller_chuyenbay?btAction=qlcb";
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                } else if (action.equals("Xóa tất cả")) {
                    bean_chuyenbay cb = new bean_chuyenbay();
                    boolean result = cb.deleteAll();
                    String url = "controller_chuyenbay?btAction=qlcb";
                    RequestDispatcher rd = request.getRequestDispatcher(url);
                    rd.forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                out.close();
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
